
package it.csttech.date;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

/**
 * OVERVIEW This class handles the conversion of every "param" according to the
 * chosen converter
 * 
 * @author marcello.felappi
 * @author marco.dalena
 */
@Provider
public class GeneralParamConverterProvider implements ParamConverterProvider {

	/**
	 * Object representing the request from the client
	 */
	@Context
	private HttpServletRequest request;

	/**
 * This method represents a constructor for the GeneralParamConverterProvider class
 */
	public GeneralParamConverterProvider() {

	}
/**
 * This method chooses the correct ParamConverter to use 
 * @param classe - the class of the object in which the string has to be transformed
 * @param type - the type of the object in which the string has to be transformed
 * @param annotations -the annotations of the object in which the string has to be transformed
 * @return the ParamConverted chosen according to the class of the object to transform to
 */
	@SuppressWarnings("unchecked")
	@Override
	public <T> ParamConverter<T> getConverter(
		Class<T> classe, Type type, Annotation[] annotations) {

		System.out.println("class in getConverter is " + classe);
		System.out.println("type in getConverter is " + type.toString());
		
		if (classe == Date.class) {
			return (ParamConverter<T>) new DateParamConverter(request);
		}

		else if (classe == Float.class) {
			return (ParamConverter<T>) new FloatParamConverter(request);
		}
		else
			return null;
	}
}
